﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.AutoCAD.GraphicsInterface;
using Autodesk.AutoCAD.Geometry;
using Galaxy.AutoCAD.Databases;
using Autodesk.AutoCAD.Runtime;

namespace Galaxy.AutoCAD.GraphicsInterface
{
    public class LeadsLabelRule : DrawableOverrule
    {
        #region 定义属性图层名，用来过滤需要重定义的多段线
        private string _layername = "Galaxy-引出标注";
        public string LayerName
        {
            get
            {
                return _layername;
            }
            set
            {
                _layername = value;
            }
        }
        #endregion
        #region 定义属性文字内容，用来给引出标注添加文字内容
        private string _textstring = "123";
        public string TextString
        {
            get
            {
                return _textstring;
            }
            set
            {
                _textstring = value;
            }
        }
        #endregion
        #region 定义属性文字内容，用来给引出标注添加文字样式
        private string _textstylename = "Standard";
        public string TextStyleName
        {
            get
            {
                return _textstylename;
            }
            set
            {
                _textstylename = value;
            }
        }
        #endregion
        #region 重载DrawableOverrule的WordDraw函数以把多段线重定义为引出标注
        public override bool WorldDraw(Drawable drawable, WorldDraw wd)
        {
            Autodesk.AutoCAD.DatabaseServices.Polyline pl = drawable as Autodesk.AutoCAD.DatabaseServices.Polyline;
            if (pl.NumberOfVertices == 3 && pl.Closed == false)
            {
                Point2dCollection pt2ds = new Point2dCollection();
                pt2ds.Add(pl.GetPoint2dAt(0));
                pt2ds.Add(pl.GetPoint2dAt(1));
                pt2ds.Add(pl.GetPoint2dAt(2));
                Autodesk.AutoCAD.DatabaseServices.Polyline resultPl = new Autodesk.AutoCAD.DatabaseServices.Polyline();
                for (int i = 0; i < pt2ds.Count; i++)
                {
                    resultPl.AddVertexAt(i, pt2ds[i], 0, 0, 0);
                }
                wd.Geometry.Polyline(resultPl, 0, 0);
                Point3d position = pl.GetPoint3dAt(1);
                Vector3d vector3dTwo = new Vector3d(pl.GetPoint3dAt(2).X - pl.GetPoint3dAt(1).X, pl.GetPoint3dAt(2).Y - pl.GetPoint3dAt(1).Y, pl.GetPoint3dAt(2).Z - pl.GetPoint3dAt(1).Z);
                Vector3d textDirection = vector3dTwo.GetNormal();
                string message = TextString;
                TextStyle textStyle = new TextStyle();
                textStyle.FromTextStyleTableRecord("Standard");
                wd.Geometry.Text(position, Vector3d.ZAxis, textDirection, message, true, textStyle);
            }
            return true;
        }
        #endregion
        #region 重载DrawableOverrule的IsApplicable函数以过滤图层名为指定的图层名
        public override bool IsApplicable(RXObject overruledSubject)
        {
            Autodesk.AutoCAD.DatabaseServices.Polyline pl = overruledSubject as Autodesk.AutoCAD.DatabaseServices.Polyline;
            if (pl != null && LayerName != null)
            {
                string layerName = pl.Layer;
                if (layerName.Contains(LayerName))
                {
                    return true;
                }
            }
            return false;
        }
        #endregion
    }
}
